import { Injectable } from "@angular/core";
import { Subject, Observable } from 'rxjs';
import { HttpService } from "http-service";
import { Tab } from '../../main/models/tab.modle';
import { API } from "../../../shared/models/api-path-extension";
import { KeyValuePairs } from "http-service/src/request";

@Injectable()
export class TabService {
  
  api: API;
  public tabChangeStatus$: Observable<Tab>;
  private tabChangeStatusSource = new Subject<Tab>();

  constructor(private httpService: HttpService) {
    this.api = new API();
    this.tabChangeStatus$ = this.tabChangeStatusSource.asObservable();
  }

  changeStatus(card: Tab) {
    this.tabChangeStatusSource.next(card);
  }

  loadTabs(): Promise<any> {
    return this.httpService.get(this.api.path({path: 'tabList' }).baseURL, null).toPromise();
  }

  saveTab(tab: Tab): Promise<any> {
    return this.httpService.post(this.api.path({path: 'tabList' }).baseURL, tab).toPromise();
  }

  rmTab(tab: Tab): Promise<any> {
    return this.httpService.delete(this.api.path({path: 'tabList' }).baseURL + '/' + tab.id).toPromise();
  }
}
